package shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import shopping.dto.RoleDto;
import shopping.entity.Role;
import shopping.service.interfaces.IRoleService;
import shopping.util.ResultCode;
import shopping.util.ResultData;

/**
 * 角色
 * @author JiangHuiJun
 *
 */
@Controller
@RequestMapping("role")
public class RoleController {

	private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);

	@Resource
	private IRoleService roleServiceImpl;

	@RequestMapping(value = "queryById.action", consumes = "application/json;charset=UTF-8")
	@ResponseBody
	public ResultData queryById(@RequestBody Role role) {
		ResultData resultData = new ResultData();
		try {
			resultData.setData(roleServiceImpl.queryById(role.getId()));
		} catch (Exception e) {
			LOGGER.info(e.getMessage());
			resultData.parse(ResultCode.SYSTEM_ERROR);
		}
		return resultData;
	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	@RequestMapping("queryPage.action")
	@ResponseBody
	public ResultData queryPage(@RequestBody RoleDto role) {
		ResultData resultData = new ResultData();
		try {
			Map<String, Object> content = new HashMap<String, Object>();
			
			if(null != role.getCode() && !"".equals(role.getCode()))
				content.put("code", role.getCode());
			if(null != role.getName() && !"".equals(role.getName()))
				content.put("name", role.getName());
			
			List<Role> list = roleServiceImpl.findByHqlPage(content, role.getFirstResult()-1, role.getMaxResult());
			int count = roleServiceImpl.countByHql(content);
			resultData.setData(list);
			resultData.setCount(count);
		} catch (Exception e) {
			resultData.parse(ResultCode.SYSTEM_ERROR);
			LOGGER.info(e.getMessage());
		}
		return resultData;
	}

}
